package deconstruction.deconTable;

import java.awt.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.ICrafting;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.InventoryBasic;
import net.minecraft.inventory.SlotCrafting;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.item.crafting.ShapedRecipes;
import net.minecraft.item.crafting.ShapelessRecipes;
import net.minecraft.inventory.InventoryCraftResult;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.inventory.Slot;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.world.World;
import cpw.mods.fml.client.FMLClientHandler;
import deconstruction.common.mod_Deconstruction;


public class ContainerDeconstructionTable extends Container{
	public InventoryDeconstructInput in = new InventoryDeconstructInput(this);
	public IInventory outputMatrix = new InventoryDeconstructResult(this, 3, 3);
	public IInventory enchantInv = new InventoryBasic("enchantInv", false, 1);
	private World worldObj;
	private int posX;
	private int posY;
	private int posZ;
	public int stackSize = 1;
	public int index;
	public static ContainerDeconstructionTable instance;

	public ContainerDeconstructionTable(InventoryPlayer par1InventoryPlayer, World par2World, int par3, int par4, int par5)
	{
		this.worldObj = par2World;
		this.posX = par3;
		this.posY = par4;
		this.posZ = par5;
		this.addSlotToContainer(new Slot(this.in, 0, 45, 21));
		this.addSlotToContainer(new SlotEnchant(par1InventoryPlayer.player, this.enchantInv, 0, 45, 46));
		this.instance = this;
		int l;
		int i1;

		for (l = 0; l < 3; ++l){
			for (i1 = 0; i1 < 3; ++i1){
				this.addSlotToContainer(new SlotInput(par1InventoryPlayer.player, this.in, this.outputMatrix, i1 + l * 3, 106 + i1 * 18, 17 + l * 18, this.enchantInv, (Container)this));
			}
		}

		for (l = 0; l < 3; ++l){
			for (i1 = 0; i1 < 9; ++i1){
				this.addSlotToContainer(new Slot(par1InventoryPlayer, i1 + l * 9 + 9, 8 + i1 * 18, 84 + l * 18));
			}
		}

		for (l = 0; l < 9; ++l){
			this.addSlotToContainer(new Slot(par1InventoryPlayer, l, 8 + l * 18, 142));
		}

		this.onCraftMatrixChanged(in);

	}


	public void onCraftMatrixChanged(IInventory inSlot)
	{
		super.onCraftMatrixChanged(inSlot);
		ItemStack[] theStacks = DeconstructionManager.getInstance().findSingleRecipe(inSlot.getStackInSlot(0), this.index);
		for(int i = 0; i<this.outputMatrix.getSizeInventory(); ++i)
			this.outputMatrix.setInventorySlotContents(i,theStacks[i]);
	}

	public void onContainerClosed(EntityPlayer par1EntityPlayer)
	{
		super.onContainerClosed(par1EntityPlayer);
		if (!this.worldObj.isRemote)
		{
			ItemStack stack = this.in.getStackInSlotOnClosing(0);
			if(stack != null){
				par1EntityPlayer.dropPlayerItem(stack);
			}
			ItemStack stack1 = this.enchantInv.getStackInSlotOnClosing(0);
			if(stack1 != null){
				par1EntityPlayer.dropPlayerItem(stack1);
			}
			if(stack == null){
				for(int i=0; i<9; i++){
					ItemStack var3 = this.outputMatrix.getStackInSlotOnClosing(i);
					if (var3 != null)
					{
						par1EntityPlayer.dropPlayerItem(var3);
					}
				}
			}
		}
	}


	public void setCurrentRecipeIndex(int currentRecipeIndex) {
		index = currentRecipeIndex;
		this.onCraftMatrixChanged(in);
	}

	@Override
	public boolean canInteractWith(EntityPlayer entityplayer) {
		return true;
	}
	public static ContainerDeconstructionTable getInstance(){
		return instance;
	}


	public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2)
    {
        ItemStack itemstack = null;
        Slot slot = (Slot)this.inventorySlots.get(par2);

        if (slot != null && slot.getHasStack())
        {
            ItemStack itemstack1 = slot.getStack();
            itemstack = itemstack1.copy();

            if (par2 == 0)
            {
                if (!this.mergeItemStack(itemstack1, 11, 47, true))
                {
                    return null;
                }

                slot.onSlotChange(itemstack1, itemstack);
            }
            else if (par2 >= 11 && par2 < 38)
            {
                if (!this.mergeItemStack(itemstack1, 38, 47, false))
                {
                    return null;
                }
            }
            else if (par2 >= 38 && par2 < 47)
            {
                if (!this.mergeItemStack(itemstack1, 11, 38, false))
                {
                    return null;
                }
            }
            else if (!this.mergeItemStack(itemstack1, 11, 47, false))
            {
                return null;
            }

            if (itemstack1.stackSize == 0)
            {
                slot.putStack((ItemStack)null);
            }
            else
            {
                slot.onSlotChanged();
            }

            if (itemstack1.stackSize == itemstack.stackSize)
            {
                return null;
            }

            slot.onPickupFromSlot(par1EntityPlayer, itemstack1);
        }

        return itemstack;
    }


	public int getCurrentRecipeIndex() {
		return this.index;
	}
}